package cn.mallss.api.controller;

import cn.mallss.api.Application;
import cn.mallss.api.dao.ExecSqlDao;
import cn.mallss.api.dao.UserDao;
import cn.mallss.beans.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
import org.springframework.beans.factory.support.BeanDefinitionDefaults;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import sun.nio.ch.ThreadPool;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;


@RestController
public class HomeController {

    @Autowired
    UserDao userDao;

    @Autowired
    ExecSqlDao execSqlDao;

    @RequestMapping("/hello")
    public String home() {
        // 开发新功能1
        // 修复bug
        return "controller/homepage2";
    }

    @GetMapping("/api/hey")
    @ResponseBody
    public String sayHello(@RequestParam(name = "id",defaultValue = "0") Integer id, HttpServletResponse response,HttpServletRequest request){

        System.out.println("123");
        List<User> user=userDao.findAll();
        return "jack:say hello;";
    }

    @RequestMapping("/api/adduser")
    @ResponseBody
    public String add(HttpServletRequest request, String name, String age, String desc){
        User user=new User();
        user.setName(name);
        user.setDesc("222");
        user.setGetup(new Date());
        userDao.save(user);
        return "添加成功";
    }

    @RequestMapping("/api/update")
    @ResponseBody
    public User update(User user){
        User save= userDao.saveAndFlush(user);
        return  save;
    }

    @RequestMapping("/api/updatename")
    @ResponseBody
    public Integer updateName(String name){

        List<Integer> ids=new LinkedList<>();
        ids.add(1);
        ids.add(2);

        Integer result= userDao.updateName(ids,name);
        return  result;
    }

    @RequestMapping("/api/get")
    @ResponseBody
    public User get(Integer id){

        List<User> save= userDao.search("ceshi","5");
//        save=userDao.findAllByIdGreaterThan0();
        return  save.isEmpty()?null:save.get(0);
    }
}
